home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1262 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  81 lines

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c,comp.lang.objective-c
  4. Subject: Re: Comma Delimited function wanted
  5. Date: 12 Jan 1996 17:16:49 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4d6521$g4r@news.iag.net>
  8. References: <4d1l42$mb6@voyager.Internex.NET> <4d3360$kg3@tahko.lpr.carel.fi>
  9. NNTP-Posting-Host: pm1-orl25.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <4d3360$kg3@tahko.lpr.carel.fi>, aril@cmt.lpr.mail.carel.fi says...
  13. >
  14. <snip>
  15. >Try something like this:
  16.  
  17. You didn't try your own code first, did you <scowling fiercely>. :-)
  18. Anyway, it will need a few modifications to work at all and a few more to 
  19. work safely.  See the rewrite below.
  20.  
  21. >
  22. >        char    buffer[] = "1,2,3,4";   /* Just an example */
  23. >        char    *p = buffer;            /* points to begin of buf */
  24. >        char    field[4][20];
  25. >        int     i, j;
  26. >
  27. >        for (j = 0; j < 4; j++) {
  28. >                memset(field[j], 0, sizeof(field[j]));  /* Initialize to 
  29. zeros */
  30. >                for (i = 0; i < sizeof(field[j]); i++, p++)
  31. >                        if (*p == ',' || !*p) {
  32. >                                field[j][i] = '\0';
  33. >                                break;
  34. >                        }
  35. >                        else
  36. >                                field[j][i] = *p;
  37. >        }
  38. >
  39.  
  40. #include <stdio.h>
  41. #include <string.h>
  42.  
  43. int main(void)
  44.    {                   
  45.    char    buffer[] = "123,,4,";   /* Make it a bit harder */
  46.    char    *p = buffer;            /* points to begin of buf */
  47.    char    field[4][20];
  48.    int     i, j;
  49.    
  50.    for (j = 0; j < 4; j++) 
  51.       {
  52. /*      memset(field[j], 0, sizeof(field[j])); ?? not necessary. */
  53.       for (i = 0; i <= sizeof(field[j]); i++, p++) /* allow for '\0'      */
  54.          if(  *p == ',' || !*p) /* these conditions could be added to the */
  55.             break;              /* for statement to eliminate these three */
  56.          else                   /* lines from the block                   */
  57.             field[j][i] = *p;
  58.  
  59.       field[j][i] = '\0';    /* need this in case i == sizeof(field[j])   */
  60.                              /* should probably handle it better, though  */
  61.       if (*p)
  62.          p++;                /* need to get past the ',' */
  63.       else
  64.          break;              /* unless it's '\0', then break */
  65.       }
  66.  
  67.    printf( "(%s)\n(%s)\n(%s)\n(%s)\n", field[0], field[1],
  68.                                        field[2], field[3]);
  69.    
  70.    return 0;
  71.    }
  72.  
  73. The possibility of parsed segment being too long for sizeof(field[0]) should 
  74. be handled a bit better. This version of the code will cause unexpected 
  75. results, if it happens.
  76.  
  77. -- 
  78. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  79. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  80.  
  81.